package acme.db;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import acme.entity.Entity;

public abstract class DAOMap<T extends Entity> implements CrudDao<T> {

	public DAOMap(){
	}
	
	@Override
	public abstract Class<T> getClazz();
	
	protected abstract Map<String, T> getMap();
	
	@Override
	public T createResource(T obj) {
		if(obj != null){
			getMap().put(obj.getId(), obj);
		}
		return obj;
	}

	@Override
	public Collection<T> getAllResources() {
		return getMap().values();
	}

	@Override
	public T readResource(String id) {
		return getMap().get(id);
	}

	@Override
	public void updateResource(T obj) {
		createResource(obj);
	}

	@Override
	public void deleteResource(String id) {
		getMap().remove(id);
	}
	
	public static class BrandDAOMap<Brand> extends DAOMap<acme.entity.Brand>{
		private static Map<String, acme.entity.Brand> map = new HashMap<String, acme.entity.Brand>();
		static{
			for(acme.entity.Brand b : DefaultCatalog.provisionBrands()){
				map.put(b.getId(), b);
			}
		}
		
		@Override
		public Class<acme.entity.Brand> getClazz() {
			return acme.entity.Brand.class;
		}
		@Override
		protected Map<String, acme.entity.Brand> getMap() {
			return map;
		}	
	}
	
	
	public static class DepartmentDAOMap<Department> extends DAOMap<acme.entity.Department>{
		private static Map<String, acme.entity.Department> map = new HashMap<String, acme.entity.Department>();
		static{
			for(acme.entity.Department d : DefaultCatalog.provisionDepartments()){
				map.put(d.getId(), d);
			}
		}

		@Override
		public Class<acme.entity.Department> getClazz() {
			return acme.entity.Department.class;
		}

		@Override
		protected Map<String, acme.entity.Department> getMap() {
			return map;
		}	
	}
	
	
	public static class ProductDAOMap<Product> extends DAOMap<acme.entity.Product>{
		private static Map<String, acme.entity.Product> map = new HashMap<String, acme.entity.Product>();
		static{
			for(acme.entity.Product p : DefaultCatalog.provisionProducts()){
				map.put(p.getId(), p);
			}
		}

		@Override
		public Class<acme.entity.Product> getClazz() {
			return acme.entity.Product.class;
		}

		@Override
		protected Map<String, acme.entity.Product> getMap() {
			return map;
		}	
	}
	
}
